package com.microstation.controller.system;

import com.microstation.common.AppConst;
import com.microstation.common.Assist;
import com.microstation.common.Result;
import com.microstation.controller.front.UserInfoController;
import com.microstation.entity.po.AdminInfo;
import com.microstation.entity.po.UserInfo;
import com.microstation.entity.po.UserSigninInfo;
import com.microstation.entity.po.view.UserInfoSigninView;
import com.microstation.entity.vo.UserInfoVO;
import com.microstation.enums.ResultEnum;
import com.microstation.service.AdminInfoService;
import com.microstation.service.UserInfoService;
import com.microstation.service.UserSigninInfoService;
import com.microstation.service.view.UserInfoSigninViewService;
import com.microstation.util.CookieUtil;
import com.microstation.util.MD5;
import com.microstation.util.MyCookie;
import com.microstation.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/api/signin")
public class SigninController extends BaseController {

    private final static Logger LOG = LoggerFactory.getLogger(SigninController.class);

    @Autowired
    private AdminInfoService adminInfoService;
    @Autowired
    private UserInfoSigninViewService userInfoSigninViewService;
    @Autowired
    private UserSigninInfoService userSigninInfoService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 调整扫码页
     *
     * @param qrcode
     * @param request
     * @return
     */
    @GetMapping(value = "/toSignin")
    public ModelAndView toSignin(String qrcode, HttpServletRequest request) {
        Cookie cookie = CookieUtil.getCookieByName(request, AppConst.COOKIE_SIGN_NAME);
        if (cookie == null) {
            return new ModelAndView("redirect:../../index/login_signin.html?qrcode=" + qrcode);
        }
        if (StringUtil.isEmpty(qrcode)) {
            return new ModelAndView("redirect:../../error.html");
        }
        return new ModelAndView("redirect:../../index/signin.html?qrcode=" + qrcode);
    }

    /**
     * 扫码人员登录
     *
     * @param account
     * @param password
     * @param response
     * @return
     */
    @PostMapping(value = "/login")
    public Result login(String account, String password, HttpServletResponse response) {
        MD5 md5 = new MD5();
        AdminInfo queryParam = new AdminInfo();
        queryParam.setAccount(account);
        queryParam.setPassword(md5.getMD5ofStr(password));
        AdminInfo adminInfo = adminInfoService.getByExample(queryParam);
        if (adminInfo == null) {
            return new Result(Boolean.TRUE, ResultEnum.NOACCOUNT, AppConst.NULL);
        }
        Cookie cookie = null;
        try {
            cookie = new MyCookie(AppConst.COOKIE_SIGN_NAME, String.valueOf(adminInfo.getId()));
            // 设置Cookie的有效期为7天
            cookie.setMaxAge(7 * 24 * 60 * 60);
            // 设置路径，这个路径即该工程下都可以访问该cookie
            // 如果不设置路径，那么只有设置该cookie路径及其子路径可以访问
            cookie.setPath("/");
            CookieUtil.addCookie(response, cookie, Boolean.TRUE);
            return new Result(Boolean.FALSE, ResultEnum.SUCCESS, AppConst.NULL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Boolean.TRUE, ResultEnum.ERROR.getType(), e.getMessage(), AppConst.NULL);
        }
    }

    /**
     * 根据二维码获取用户信息
     *
     * @param qrcode
     * @return
     */
    @PostMapping(value = "/getUserByQRCode")
    public Result getUserByQRCode(@RequestParam(value = "qrcode", defaultValue = "") String qrcode) {
        try {
            UserInfoSigninView queryParam = new UserInfoSigninView();
            queryParam.setQrcode(qrcode);
            UserInfoSigninView userInfoSigninView = userInfoSigninViewService.getByExample(queryParam);
            return new Result(ResultEnum.SUCCESS.getType(), userInfoSigninView);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Boolean.TRUE, ResultEnum.ERROR.getType(), e.getMessage(), AppConst.NULL);
        }
    }

    /**
     * 签到
     *
     * @param qrcode
     * @param id
     * @return
     */
    @PostMapping(value = "/signin")
    public Result signin(@RequestParam(value = "qrcode", defaultValue = "") String qrcode, Long id) {
        try {
            UserInfo userInfo = userInfoService.findOne(id);
            if (!qrcode.equals(userInfo.getQrcode())) {
                return new Result(Boolean.TRUE, ResultEnum.FALSE, AppConst.NULL);
            }
            UserSigninInfo saveParam = new UserSigninInfo();
            saveParam.setActId(userInfo.getActId());
            saveParam.setUserId(userInfo.getId());
            userSigninInfoService.saveNotNull(saveParam);
            return new Result(Boolean.FALSE, ResultEnum.SUCCESS, AppConst.NULL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Boolean.TRUE, ResultEnum.ERROR, AppConst.NULL);
        }
    }
}
